package com.dldata.devtools30.controller.api;


import com.dldata.devtools30.entity.DevtApiMainEntity;
import com.dldata.devtools30.entity.DevtApiTypeEntity;
import com.dldata.devtools30.service.api.DevtApiMainEntityService;
import com.dldata.devtools30.service.api.DevtApiTypeEntityService;
import com.dldata.devtools30.utils.StringUtil;
import org.slf4j.Logger;
import org.slf4j.LoggerFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.Model;
import org.springframework.web.bind.annotation.RequestMapping;

import java.util.List;
import java.util.UUID;

@Controller
@RequestMapping(value = "/DevtApiMainEntityController")
public class V_DevtApiMainEntityController {
    private Logger logger = LoggerFactory.getLogger(this.getClass());
    @Autowired
    DevtApiTypeEntityService devtApiTypeEntityService;
    @Autowired
    DevtApiMainEntityService devtApiMainEntityService;

    @RequestMapping(value = "/toPage")
    public String toPage(){
        return "thymeleaf/devtools30/api/v_DevtApiTypeSub";
    }

    @RequestMapping(value = "/edit")
    public String edit(String id,String pid,Model model){
        DevtApiMainEntity pFunctionmainEntity = null;
        if(!StringUtil.isNullOrSpace(id)){
            pFunctionmainEntity = devtApiMainEntityService.findOne(id);
        }
        if (pFunctionmainEntity == null) {
            pFunctionmainEntity = new DevtApiMainEntity();
            pFunctionmainEntity.setId(UUID.randomUUID().toString());
            pFunctionmainEntity.setFlid(pid);
            final List<DevtApiMainEntity> pFunctionmainEntities = devtApiMainEntityService.getAllData();
            pFunctionmainEntity.setXh(pFunctionmainEntities.size()+1);
            for (int i = 0; i < pFunctionmainEntities.size(); i++) {
                DevtApiMainEntity functionmainEntity = pFunctionmainEntities.get(i);
                if(functionmainEntity.getXh()!=null&&functionmainEntity.getXh()>=pFunctionmainEntity.getXh()){
                    pFunctionmainEntity.setXh(functionmainEntity.getXh()+1);
                }
            }
        }
        model.addAttribute("data",pFunctionmainEntity);

        List<DevtApiTypeEntity> mainTypeList = devtApiTypeEntityService.getAllData();
        model.addAttribute("TypeList",mainTypeList);
        return "thymeleaf/devtools30/api/v_DevtApiTypeSubEdit";
    }
}
